package com.neusoft.neuiotms.dcom.service.impl;

import java.util.List;

import javax.persistence.EntityNotFoundException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.converter.ProjectConverter;
import com.neusoft.neuiotms.dcom.dto.ProjectDTO;
import com.neusoft.neuiotms.dcom.model.Project;
import com.neusoft.neuiotms.dcom.repository.ProjectRepository;
import com.neusoft.neuiotms.dcom.service.ProjectService;

@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ProjectConverter projectConverter;
    @Autowired
    private ProjectRepository projectRepository;

    @Override
    public List<ProjectDTO> index(final Searchable searchable) {
        final List<Project> result = this.projectRepository.index(searchable);
        return this.projectConverter.toListDTO(result);
    }

    @Override
    public Page<ProjectDTO> page(final Searchable searchable, final Pageable pageable) {
        final Page<Project> result = this.projectRepository.page(searchable, pageable);
        return this.projectConverter.toPageDTO(result);
    }

    @Override
    public ProjectDTO create(final ProjectDTO dto) {

        final Project model = this.projectConverter.toModel(dto);
        this.projectRepository.save(model);

        return this.projectConverter.toDTO(model);
    }

    @Override
    public ProjectDTO update(final Long id, final ProjectDTO dto) {

        Project model;
        try {
            model = this.projectRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        dto.setId(model.getId());
        model = this.projectConverter.toModel(dto);
        this.projectRepository.save(model);
        return this.projectConverter.toDTO(model);
    }

    @Override
    public ProjectDTO detail(final Long id) {

        Project model;
        try {
            model = this.projectRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }
        return this.projectConverter.toDTO(model);
    }

    @Override
    public ProjectDTO getInfoByCode(final String code) {

        final Project model = this.projectRepository.findByCode(code);
        if (null == model) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {code });
        }
        return this.projectConverter.toDTO(model);
    }

    @Override
    public void delete(final Long id) {

        this.projectRepository.deleteById(id);

    }

}
